﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using SisCRA.Classes;
using SisCRA.Database;

namespace SisCRA.Business
{
    public class StudentBLL
    {
        #region -Public Methods-

        /// <summary>
        /// Método que chama a camada de dados para salvar um aluno. 
        /// Caso o aluno já exista chama o método de Update, senão chama o método de Insert.
        /// </summary>
        /// <param name="student">Objeto Aluno</param>
        public void SaveStudent(Student student)
        {
            try
            {
                StudentDAL studentDAL = new StudentDAL();

                if (student.Id > 0)
                {
                    studentDAL.UpdateStudent(student.Id, student.Code.ToUpper(), student.Name.ToUpper(), student.Email.ToUpper());
                }
                else
                {
                    studentDAL.InsertStudent(student.Code.ToUpper(), student.Name.ToUpper(), student.Email.ToUpper());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para buscar aluno(s) pelos parêmetros passados.
        /// </summary>
        /// <param name="student">Objeto Aluno</param>
        /// <returns>DataTable contendo a busca realizada</returns>
        public DataTable GetStudentByParam(Student student)
        {
            try
            {
                StudentDAL studentDAL = new StudentDAL();

                if (student.Id > 0)
                {
                    return studentDAL.GetStudentById(student.Id);
                }
                else if (!string.IsNullOrWhiteSpace(student.Code))
                {
                    return studentDAL.GetStudentByCode(student.Code);
                }
                else if (!string.IsNullOrWhiteSpace(student.Name))
                {
                    return studentDAL.GetStudentByName(student.Name);
                }
                else
                {
                    return studentDAL.GetAll();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para buscar todos os alunos.
        /// </summary>
        /// <returns>DataTable contendo a busca realizada</returns>
        public DataTable GetAllStudent()
        {
            try
            {
                StudentDAL studentDAL = new StudentDAL();
                return studentDAL.GetAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
